package com.ruoyi.web.controller.store;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.stage.domain.StuStage;
import com.ruoyi.store.domain.StuQuesAnwser;
import com.ruoyi.store.domain.StuQuestion;
import com.ruoyi.store.domain.StuQuestionStore;
import com.ruoyi.store.service.IStuQuesAnwService;
import com.ruoyi.store.service.IStuQuestionService;
import com.ruoyi.store.service.IStuQuestionStoreService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目管理
 */
@RestController
@RequestMapping("store/ques")
public class StuQuestionController extends BaseController {
    @Resource
    private IStuQuestionService stuQuestionService;

    @Resource
    private IStuQuestionStoreService stuQuestionStoreService;

    @Resource
    private IStuQuesAnwService stuQuesAnwService;

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('store:ques:list')")
    public TableDataInfo list(StuQuestion stuQuestion)
    {
        startPage();
        List<StuQuestion> list = stuQuestionService.selectStuQuestionList(stuQuestion);
        return getDataTable(list);
    }

    /**查询阶段详细信息，实现动态加载
     *
     *
     */
    @GetMapping(value = { "/", "/{quesId}" })
    @PreAuthorize("@ss.hasPermi('store:list:query')")
    public AjaxResult getInfo(@PathVariable(value = "quesId", required = false) Long quesId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("stores",stuQuestionStoreService.selectAllStores());
        if (StringUtils.isNotNull(quesId))
        {
            StuQuestion stuQuestion = stuQuestionService.selectQuesById(quesId);
            StuQuesAnwser stuQuesAnwser = stuQuesAnwService.selectAnwSerById(quesId);
            stuQuestion.setSelContA(stuQuesAnwser.getSelContA());
            stuQuestion.setSelContB(stuQuesAnwser.getSelContB());
            stuQuestion.setSelContC(stuQuesAnwser.getSelContC());
            stuQuestion.setSelContD(stuQuesAnwser.getSelContD());
            stuQuestion.setAnswer(stuQuesAnwser.getAnwser());
            String manyAnswer = stuQuesAnwser.getManyAnswer();
            //将字符串分割成字符串数组
            if (manyAnswer != null){
                String[] manyAnsers = manyAnswer.split("");
                stuQuestion.setManyAnswer(manyAnsers);
            }
            ajax.put(AjaxResult.DATA_TAG, stuQuestion);
            ajax.put("storeIds", stuQuestionStoreService.selectStuStoresListByQuesId(quesId));
        }
        return ajax;
    }

    /**
     * 添加题目
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('store:ques:add')")
    public AjaxResult add(@RequestBody StuQuestion stuQuestion){
        String username = getUsername();
        stuQuestion.setCreateBy(username);

        return toAjax(stuQuestionService.insertStuQuestion(stuQuestion));
    }

    /**修改题目*
     *
     * @param stuQuestion
     * @return
     */
    @PutMapping
    @PreAuthorize("@ss.hasPermi('store:ques:edit')")
    public  AjaxResult edit(@Validated @RequestBody StuQuestion stuQuestion){
//        if (UserConstants.NOT_UNIQUE.equals(stuQuestionStoreService.checkStoreNameUnique(stuQuestionStore)))
//        {
//            return AjaxResult.error("修改题库'" + stuQuestionStore.getStoreName() + "'失败，题库名称已存在");
//        }
//        stuQuestionStore.setUpdateBy(getUsername());
        return toAjax(stuQuestionService.updateStuQuestion(stuQuestion));
    }

    @DeleteMapping("/{quesIds}")
    @PreAuthorize("@ss.hasPermi('store:ques:remove')")
    public AjaxResult remove(@PathVariable Long[]quesIds){
        return  toAjax(stuQuestionService.delStuQuesByIds(quesIds));
    }



}
